import {useChatStore} from '@/store/modules/chatStore.js'
import {findFriendOrAddById} from '@/api/im'
import { useToast } from 'wot-design-uni'
export function useChatWithFriend() {
  const chatStore = useChatStore()
  const toast = useToast()
  async function goChatById(friendId: string) {
    const res = await findFriendOrAddById({friendId})
    if (res.code === 0) {
      const { nickname, headImage, id } = res.data
      const chat = {
        type: 'PRIVATE',
        targetId: id,
        showName: nickname,
        headImage: headImage
      }
      chatStore.openChat(chat)
      const chatIdx = chatStore.findChatIdx(chat)
      uni.navigateTo({
				url: "/pages-sub/chatBox/index?chatIdx=" + chatIdx
			})
    } else {
      toast.error(res.msg)
    }
  }
  return {
    goChatById
  }
}